package cn.com.miao.lynx.common.core.convert

import javax.persistence.AttributeConverter

/**
 * AbstractEnumConverter1
 * @title: AbstractEnumConverter1
 * @description:
 * @author: dengmiao
 * @create: 2019-12-27 16:50
 **/
abstract class AbstractEnumConverter<ATTR, DB> : AttributeConverter<ATTR, DB> where ATTR: Enum<ATTR>, ATTR: PersistEnum2DB<DB> {

    var clazz: Class<ATTR>


    constructor(clazz: Class<ATTR>) {
        this.clazz = clazz
    }

    override fun convertToDatabaseColumn(p0: ATTR): DB {
        return p0.getData()
    }

    override fun convertToEntityAttribute(p0: DB): ATTR? {
        if (p0 == null) {
            return null;
        }
        var filter = clazz.enumConstants.filter { e -> e.getData() == p0 }
        return if (filter.isNotEmpty()) {
            filter[0]
        } else {
            null
        }

    }
}
